<?php
/**
 *	  商品模块
 *	  [Jsen.net] (C)2013-2099 Jason Group Science and technology co., LTD.
 *	  This is NOT a freeware, use is subject to license terms
 *
 *	  http://www.jsen.net
 *	  tel:400-681-9511
 */
class IndexController extends HomeBaseController {
	public function _initialize() {
		parent::_initialize();
		$this->db = model('Goods');
		$this->goods_category_db = model('Admin/Category');
		$this->goods_categorys = getcache('goods_category', 'goods');
	}
	/* 首页 */
	public function index() {
		// feeReward();
		// die;
// testAddUser();	
// die;
// updateInviteAllRate(1,1);
// die;
// echo bcdiv(0.44, 86400,8);
// echo "<br>";
// echo bcdiv(0.4488, 86400,8);
// $rut = getUserLevel();
// p($rut);
// 		die;
		$SEO = seo();
		$GPS="home";
		if($_SESSION["ad"]!=1){
			$_SESSION["ad"]=1;
		}else{
			$show_ad=1;
		}
		$union_new = m('union')->order("id desc")->limit(8)->select();
		include template('index');
	}
	public function testBuy(){
		G('begin');
		$list = M("user")->field("id,invite_id,user_type,invite_str")->select();
		$randKey = array_rand($list);
		$user = $list[$randKey];
		$miner_type = mt_rand(1,4);
		$m_count = M("miner")->where("user_id=".$user['id']." and type=".$miner_type." and status=1 ")->count();
		$maxHas = C("maxhas");
		if($m_count>$maxHas[$user['user_type']][$miner_type]){
			echo "达到上限";
			die;
		}
		$oid = testBuyMiner($user['id'],2,2);
		if($user['miner_type']<$miner_type){
			M("user")->where("id=".$user['id'])->setField("miner_type",$miner_type);
		}
		//直推奖励 给
		rateInvite($user['invite_id'],$user['id'],$miner_type,$oid);
		// 更新上级开采率  遍历
		updateInviteAllRate($user['invite_str'],$miner_type);
		//检测上级是否达到升级条件 遍历
		checkUserUp($user['invite_str']);
		G('end');
		echo '本次任务共执行'.G('begin','end')."秒{$info['invite_str']}";exit;
	}
	/**
	 * 矿机列表
	 */
	public function lists() {
		$miner = C("miner");
		include template('goods_list');
	}
	public function ranking(){
		// $list = M("user")->field("invite_id,count(id) as count")->where("invite_id>0")->group("invite_id")->order("count desc")->limit(10)->select();
		$list = M("user")->field("id,invite_count as count")->where("id<>219")->order("invite_count desc")->limit(10)->select();
		// p($list);
		foreach ($list as $key => $value) {
			$u = M("user")->field("username,ico")->find($value['id']);
			$list[$key]['username'] = $u['username'];
			$list[$key]['ico'] = $u['ico'];
		}
		include template('ranking');
	}
}